package view.Bag
{
	import flash.display.BitmapData;
	import flash.display.Shape;
	
	import starling.display.Image;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.text.TextField;
	import starling.textures.Texture;
	import starling.utils.HAlign;
	
	public class BagPanelView extends Sprite
	{
		private var _dragable:Boolean = true;
		private var _titleBar:Quad;
		private var _titleTxt:TextField;
		private var _bagPanelBg:Quad;
		private static const PANEL_WIDTH:uint = 480;
		private static const PANEL_HEIGHT:uint = 450;
		private static const ITEM_LEN:uint = 60;
		private static const ROW_NUM:uint = 5;
		private static const COL_NUM:uint = 7;
		private static const START_X:uint = 30;
		private static const START_Y:uint = 120;
		private var _gridContainer:Shape;
		public function BagPanelView()
		{
			super();
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		private function onAddedToStage(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			init();
		}
		
		private function init():void{
			drawBagPanelBg();
			drawTitleBar();
			drawTitleTxt();
			drawGrid();
		}
		
		private function drawGrid():void
		{
			_gridContainer = new Shape();
			_gridContainer.graphics.lineStyle(1, 0x0033ff);
			for(var i:int = 0;i< ROW_NUM + 1;i++){
				_gridContainer.graphics.moveTo(0, ITEM_LEN * i);
				_gridContainer.graphics.lineTo(ITEM_LEN * COL_NUM, ITEM_LEN * i);
				for(var j:int = 0;j< COL_NUM + 1;j++){
					_gridContainer.graphics.moveTo(ITEM_LEN * j,0);
					_gridContainer.graphics.lineTo(ITEM_LEN * j, ITEM_LEN * ROW_NUM + 1);
				}
			}
			
			var bmd:BitmapData = new BitmapData(ITEM_LEN * COL_NUM , ITEM_LEN * ROW_NUM, true, 0);
			bmd.draw(_gridContainer);
			var texture:Texture = Texture.fromBitmapData(bmd);
			var img:Image = new Image(texture);
			addChild(img);
			img.x = START_X;
			img.y = START_Y;
		}
		
		private function drawTitleTxt():void
		{
			_titleTxt = new TextField(300, 50, "BAG", "Verdana", 30, 0xffffff, true);
			_titleTxt.x = 90;
			_titleTxt.y = 10;
			_titleTxt.autoScale = true;
			_titleTxt.hAlign = HAlign.CENTER;
			addChild(_titleTxt);
		}
		
		private function drawTitleBar():void
		{
			_titleBar = new Quad(PANEL_WIDTH, 90, 0x0033ff,true);
			addChild(_titleBar);
		}
		
		private function drawBagPanelBg():void
		{
			_bagPanelBg = new Quad(PANEL_WIDTH, PANEL_HEIGHT, 0x0099ff, true);
			addChild(_bagPanelBg);
		}
	}
}